package yr.website

import grails.converters.JSON

import java.text.SimpleDateFormat

class LoggController {

    def getAll() {
        def data = [:]
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd")
        def page = Integer.parseInt("${params.page ?: 1}")
        def pagesize = Integer.parseInt("${params.pagesize ?: 20}")
        def recordor = "${params.recordor ?: ''}"
        def recordDate1 = "${params.recordDate1 ?: ''}"
        def recordDate2 = "${params.recordDate2 ?: ''}"
        def a = Logg.createCriteria()
        def list = a.list(max: pagesize, offset: (page - 1) * pagesize) {
            and {
                if (!recordor.isEmpty()) {
                    eq("recordor", recordor)
                }
                if (!recordDate1.isEmpty()) {
                    ge("recordDate", new Date(Long.parseLong(recordDate1)))
                }
                if (!recordDate2.isEmpty()) {
                    def start = sdf2.format(new Date(Long.parseLong(recordDate2)))
                    Date end = sdf1.parse(start + " 23:59:59")
                    le("recordDate", end)
                }
            }
            order("recordDate", "desc")
        }
        def b = Logg.createCriteria()
        def length = b.get {
            and {
                if (!recordor.isEmpty()) {
                    eq("recordor", recordor)
                }
                if (!recordDate1.isEmpty()) {
                    ge("recordDate", new Date(Long.parseLong(recordDate1)))
                }
                if (!recordDate2.isEmpty()) {
                    def start = sdf2.format(new Date(Long.parseLong(recordDate2)))
                    Date end = sdf1.parse(start + " 23:59:59")
                    le("recordDate", end)
                }
            }
            projections {
                rowCount()
            }
        }
        if (list) {
            data << [statuCode: "0", errMsg: "0", data: list, total: length]
        } else {
            data << [statuCode: "1", errMsg: "1"]
        }
        render data as JSON
    }
}
